package com.atguigu.mr.nlineInputFormat;

import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;

import java.io.IOException;

/*** LongWritable:输入数据的key
 * Text:输入数据的value
 * Text:输出数据key类型
 * IntWritable:输出value的类型 按照NLineFormat来切分数据
 * @author baojinlong
 */
public class WordcountMapper extends Mapper<LongWritable, Text, Text, IntWritable> {
    private IntWritable tmpValue = new IntWritable(1);
    private Text tmpKey = new Text();

    @Override
    protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
        // 获取一行转化成String
        String line = value.toString();
        // 切割
        String[] words = line.split(" ");
        // 循环遍历输出
        for (String word : words) {
            tmpKey.set(word);
            context.write(tmpKey, tmpValue);
        }
    }
}
